package com.client;

import java.awt.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JDialog;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextArea;
import javax.swing.JButton;

import com.metier.logique.DateDsl;
import com.metier.logique.MSG;
import com.metier.persistance.Poi;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class DialogDetailsPOI extends JDialog implements ActionListener {
	private JTextField txtNom;
	private JTextArea txtDescription;
	private JButton btnMettreJour;
	private JLabel lblDescription;
	private JLabel lblNomDuPoi;
	private JLabel lblPOI;
	private JButton btnAnnuler;
	
	
	public DialogDetailsPOI() {
		
		buildDialog();
	}
	
	public DialogDetailsPOI(Component oComp) {
		setSize(280, 300);
		lblPOI = (JLabel) oComp;
		buildDialog();
		setTitle(lblPOI.getToolTipText());
		txtNom.setText(lblPOI.getToolTipText());
		
		MSG oMSG = new MSG();
		Poi oPOI = new Poi();
		
		oMSG.setData("method", "findById");
		oMSG.setData("id", lblPOI.getName());
		oMSG = oPOI.find(oMSG);
		
		System.out.println("dd");
		oPOI = (Poi) oMSG.getData("donnees");
		txtDescription.setText(oPOI.getDescriptif());
	}
	
	public void buildDialog(){
		setResizable(false);
		setModal(true);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		
		lblNomDuPoi = new JLabel("Nom du POI :");
		
		txtNom = new JTextField();
		lblNomDuPoi.setLabelFor(txtNom);
		txtNom.setColumns(10);
		
		lblDescription = new JLabel("Description : ");
		
		txtDescription = new JTextArea();
		lblDescription.setLabelFor(txtDescription);
		
		btnMettreJour = new JButton("Mettre à jour");
		btnMettreJour.addActionListener(this);
		
		btnAnnuler = new JButton("Annuler");
		btnAnnuler.addActionListener(this);
		
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(lblNomDuPoi)
						.addComponent(txtNom, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblDescription)
						.addComponent(txtDescription, GroupLayout.PREFERRED_SIZE, 239, GroupLayout.PREFERRED_SIZE)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(btnMettreJour)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(btnAnnuler)))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblNomDuPoi)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(txtNom, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(lblDescription)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(txtDescription, GroupLayout.PREFERRED_SIZE, 96, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnMettreJour)
						.addComponent(btnAnnuler))
					.addContainerGap(86, Short.MAX_VALUE))
		);
		getContentPane().setLayout(groupLayout);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		if (source == btnMettreJour) {
			MSG oMSG = new MSG();
			Poi oPOI = new Poi();
			SimpleDateFormat formater = new SimpleDateFormat("yy-MM-dd HH:mm:ss"); 
			Date date = new Date(); 
			
			oMSG.setData("id", lblPOI.getName());
			oMSG.setData("NOM", txtNom.getText());
			oMSG.setData("DESCRIPTIF", txtDescription.getText());
			oMSG.setData("DATE_MODIFICATION", formater.format(date));
			oMSG = oPOI.update(oMSG);
		}
		setVisible(false);
	}
}
